var mysql = require('mysql');
var async = require('async');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '159456',
    database: 'longzs',
    debug: false
});

function zqnewsmysql() {
    console.log("index");
}


exports.getrs = function (host, cb, host2) {
    if (host == undefined) return;
    host2 = (host == undefined || host2 == "") ? host : host2;
    connection.connect(function (err, results) {
        connection.query("SELECT * FROM longzs_re where host='" + host + "' and keyword='nr'", function (err, result) {
            if(err){
                console.log(err);
            }
            if (result != undefined) {
                connection.query("SELECT gbk FROM longzs_gbk where host='" + host2 + "'", function (err, result2) {
                    if (result2[0] != undefined&&result2[0].gbk != undefined) {
                        if(result[0]==undefined) result[0]=new Array();
                        result[0]['gbk'] = result2[0].gbk;
                    }
                    cb(result[0]);
                });
            }
        });
    });
};

exports.setgbk = function (host, isgbk) {
    async.waterfall([
            function (cb3) {
                connection.query("SELECT id FROM longzs_gbk where host='" + host + "'", function (err, result) {
                    cb3(err, isgbk,result);
                })
            },

            function (isgbk,result, cb3) {
                isgbk=(isgbk=="gbk"||isgbk=="gb2312")?1:0;
                if (result == undefined||result.length<1) {
                    connection.query("insert into longzs_gbk (`gbk`,`host`) values ('"+isgbk+"','"+host+"')", function (err, result) {
                    });
                } else {
                    connection.query("update longzs_gbk set `gbk`='"+isgbk+"' where `host`='" +host+"'", function (err, result) {
                    });
                }
            }
        ],
        function (err, results) {
            console.log("dddd");
        }
    )
}
